package com.yangluyao.test.lockDemo;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author YLY
 * @ClassName CyclicBarrierDemo  设置线程大小，集齐线程数量之后，执行相应方法
 * @Date 2020/5/7
 * @Version 1.0.2
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier =    new CyclicBarrier(3000,()->{
            System.out.println("集齐龙珠召唤神龙");
        });

        for (int i = 1; i <=3000 ; i++) {
            final int  tempInt = i;
            new Thread(()->{
                System.out.println("收集到第："+tempInt+"颗龙珠");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
